#!/bin/bash

# 用户注册API测试脚本
# 对应Python项目的users/register路由功能

BASE_URL="http://localhost:8080"
API_BASE="$BASE_URL/users"

echo "=== 用户注册API测试 ==="
echo "API Base URL: $API_BASE"
echo

# 测试1: 正常用户注册
echo "测试1: 正常用户注册"
REGISTER_DATA='{
  "user_name": "testuser001",
  "email": "testuser001@example.com",
  "password": "password123",
  "password_repeat": "password123",
  "captcha_code": "1234",
  "real_name": "测试用户001",
  "phone": "13800138001",
  "enter_name": "测试企业"
}'

echo "请求数据: $REGISTER_DATA"
echo "响应:"
curl -X POST "$API_BASE/register" \
  -H "Content-Type: application/json" \
  -d "$REGISTER_DATA" \
  -w "\nHTTP状态码: %{http_code}\n" \
  -s
echo
echo "---"
echo

# 测试2: 用户名已存在
echo "测试2: 用户名已存在"
REGISTER_DATA_DUPLICATE='{
  "user_name": "testuser001",
  "email": "testuser002@example.com",
  "password": "password123",
  "password_repeat": "password123",
  "captcha_code": "1234",
  "real_name": "测试用户002"
}'

echo "请求数据: $REGISTER_DATA_DUPLICATE"
echo "响应:"
curl -X POST "$API_BASE/register" \
  -H "Content-Type: application/json" \
  -d "$REGISTER_DATA_DUPLICATE" \
  -w "\nHTTP状态码: %{http_code}\n" \
  -s
echo
echo "---"
echo

# 测试3: 邮箱已存在
echo "测试3: 邮箱已存在"
REGISTER_DATA_EMAIL_DUPLICATE='{
  "user_name": "testuser003",
  "email": "testuser001@example.com",
  "password": "password123",
  "password_repeat": "password123",
  "captcha_code": "1234",
  "real_name": "测试用户003"
}'

echo "请求数据: $REGISTER_DATA_EMAIL_DUPLICATE"
echo "响应:"
curl -X POST "$API_BASE/register" \
  -H "Content-Type: application/json" \
  -d "$REGISTER_DATA_EMAIL_DUPLICATE" \
  -w "\nHTTP状态码: %{http_code}\n" \
  -s
echo
echo "---"
echo

# 测试4: 密码不一致
echo "测试4: 密码不一致"
REGISTER_DATA_PASSWORD_MISMATCH='{
  "user_name": "testuser004",
  "email": "testuser004@example.com",
  "password": "password123",
  "password_repeat": "password456",
  "captcha_code": "1234",
  "real_name": "测试用户004"
}'

echo "请求数据: $REGISTER_DATA_PASSWORD_MISMATCH"
echo "响应:"
curl -X POST "$API_BASE/register" \
  -H "Content-Type: application/json" \
  -d "$REGISTER_DATA_PASSWORD_MISMATCH" \
  -w "\nHTTP状态码: %{http_code}\n" \
  -s
echo
echo "---"
echo

# 测试5: 敏感词用户名
echo "测试5: 敏感词用户名"
REGISTER_DATA_SENSITIVE='{
  "user_name": "root",
  "email": "testuser005@example.com",
  "password": "password123",
  "password_repeat": "password123",
  "captcha_code": "1234",
  "real_name": "测试用户005"
}'

echo "请求数据: $REGISTER_DATA_SENSITIVE"
echo "响应:"
curl -X POST "$API_BASE/register" \
  -H "Content-Type: application/json" \
  -d "$REGISTER_DATA_SENSITIVE" \
  -w "\nHTTP状态码: %{http_code}\n" \
  -s
echo
echo "---"
echo

# 测试6: 邀请注册
echo "测试6: 邀请注册"
REGISTER_DATA_INVITATION='{
  "user_name": "testuser006",
  "email": "testuser006@example.com",
  "password": "password123",
  "password_repeat": "password123",
  "captcha_code": "1234",
  "real_name": "测试用户006",
  "register_type": "invitation",
  "value": "team123"
}'

echo "请求数据: $REGISTER_DATA_INVITATION"
echo "响应:"
curl -X POST "$API_BASE/register" \
  -H "Content-Type: application/json" \
  -d "$REGISTER_DATA_INVITATION" \
  -w "\nHTTP状态码: %{http_code}\n" \
  -s
echo
echo "---"
echo

# 测试7: 检查用户是否存在
echo "测试7: 检查用户是否存在"
echo "检查用户名: testuser001"
curl -X GET "$API_BASE/check-user/testuser001" \
  -w "\nHTTP状态码: %{http_code}\n" \
  -s
echo
echo "检查用户名: nonexistentuser"
curl -X GET "$API_BASE/check-user/nonexistentuser" \
  -w "\nHTTP状态码: %{http_code}\n" \
  -s
echo
echo "---"
echo

echo "=== 测试完成 ==="
